/**  
 * 
 */
package com.astrocsr.dao;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Query;
import org.springframework.stereotype.Repository;

import com.astrocsr.domain.bean.TblModule;

@Repository("moduleDAO")
public class ModuleDAO extends BaseHelperDAO {
	@SuppressWarnings("unchecked")
	public List<TblModule> listModule() throws Exception {
		List<TblModule> result = new ArrayList<TblModule>();
		try {

			String stringQuery = "from TblModule order by moduleCode)";
			Query query = getCurrentSession().createQuery(stringQuery);
			result = query.list();

		} catch (Exception e) {
			throw e;
		}
		return result;

	}
	
	public List<TblModule> listTblModule_audit() throws Exception {
		List<TblModule> result = new ArrayList<TblModule>();
		try {

			String stringQuery = "from TblModule where moduleUri IS NOT NULL order by moduleName)";
			Query query = getCurrentSession().createQuery(stringQuery);
			result = query.list();

		} catch (Exception e) {
			throw e;
		}
		return result;

	}
	
	public List<TblModule> listParent() throws Exception {
		List<TblModule> result = new ArrayList<TblModule>();
		try {

			String stringQuery = "from TblModule where parent IS NULL";
			Query query = getCurrentSession().createQuery(stringQuery);
			result = query.list();

		} catch (Exception e) {
			throw e;
		}
		return result;

	}

	@SuppressWarnings("unchecked")
	public List<TblModule> listTblModule_end(String val) throws Exception {
		List<TblModule> result = new ArrayList<TblModule>();
		try {
			String stringQuery = "from TblModule  where moduleCode  NOT IN (select t.moduleCode from TblPermission t where t.roleCode = :ident) order by moduleCode";
			Query query = getCurrentSession().createQuery(stringQuery).setParameter("ident", val);
			result = query.list();

		} catch (Exception e) {
			throw e;
		}
		return result;

	}
	
	@SuppressWarnings("unchecked")
	public List<TblModule> listModuleByRole(String role) throws Exception {
		List<TblModule> result = null;
		try {
			String stringQuery = "select am from TblModule am, TblPermission rp where am.moduleCode=rp.moduleCode and rp.groupCode=:groupCode order by am.orderBy ";
			Query query = getCurrentSession().createQuery(stringQuery);
			
			query.setParameter("groupCode", role);
			result = query.list();

		} catch (Exception e) {
			throw e;
		}
		return result;

	}
	
	public void addModule(TblModule entity) {
		// Insert to database
		getCurrentSession().save(entity);
	}

	public void deleteModule(String moduleCode) {
		Query query = getCurrentSession().createQuery("DELETE from TblModule WHERE moduleCode =:moduleCode ");
		query.setParameter("moduleCode", moduleCode);
		query.executeUpdate();
	}

	
	public TblModule getModuleCode(String mID) throws Exception { 
		Query query = getCurrentSession().createQuery("FROM TblModule WHERE moduleCode =:ident ").setParameter("ident", mID);
		query.setMaxResults(1);
		List<TblModule> list = query.list();

		return (list != null && list.size() > 0)? list.get(0): null;
	}
	
	public TblModule slModuleCode(String mID) throws Exception { 
		Query query = getCurrentSession().createQuery("FROM TblModule WHERE moduleCode =:ident ").setParameter("ident", mID);
		query.setMaxResults(1);
		List<TblModule> list = query.list();

		return (list != null && list.size() > 0)? list.get(0): null;
	}
}
